package controller

import (
	"fmt"
	"net/http"
	"github.com/gin-gonic/gin"
)

type User struct {
	Name string `form:"name" json:"name" uri:"name" binding:"required`
	Id string `form:"id" json:"id" uri:"id" binding:"required`
	Sex string `form:"sex" json:"sex" uri:"sex" binding:"required`
}


func UserList (c *gin.Context) {
	name := c.DefaultQuery("post_methond", "get")
	c.String(200, fmt.Sprintf("post_method %s\n", name))
 }

 func GetUser (c *gin.Context) {
	name := c.Param("name")
	id := c.Param("id")
	c.String(200, fmt.Sprintf("userinfo %s, %s\n", name, id))
 }

 func GetUserForm (c *gin.Context) {
	name := c.DefaultPostForm("name", "zhangsan")
	id := c.PostForm("id")
	sex := c.PostForm("sex")
	c.String(200, fmt.Sprintf("userinfo %s, %s,%s\n", name, id, sex))
 }
 // 绑定JSON参数到User
 func GetUserBindJson (c *gin.Context) {
	var ujson User
	if err := c.ShouldBindJSON(&ujson); err!=nil {
		c.JSON(http.StatusBadRequest, gin.H{"error":err.Error()})
		return 
	}
	c.JSON(http.StatusOK, ujson)
 }
  // 绑定Form参数到User
 func GetUserBindForm (c *gin.Context) {
	var ujson User
	if err := c.Bind(&ujson); err!=nil {
		c.JSON(http.StatusBadRequest, gin.H{"error":err.Error()})
		return 
	}
	c.JSON(http.StatusOK, ujson)
 }
  // 绑定Uri参数到User
 func GetUserBindUri (c *gin.Context) {
	var ujson User
	if err := c.ShouldBindUri(&ujson); err!=nil {
		c.JSON(http.StatusBadRequest, gin.H{"error":err.Error()})
		return
	}
	c.JSON(http.StatusOK, ujson)
 }